import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/bean/user.dart';

class UserPage extends StatefulWidget {
  const UserPage({super.key});

  @override
  State<UserPage> createState() => _UserPageState();
}

class _UserPageState extends State<UserPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("引导页")),
      body: Center(
        child: TextButton(
          onPressed: () {
            toMap();
          },
          child: Text("点击", style: TextStyle(fontSize: 20)),
        ),
      ),
    );
  }

  void toMap() {
    // JSON数据字符串
    String jsonString = '{"NAME": "", "AGE": 30}';
    Map<String, dynamic> userMap = jsonDecode(jsonString);
    print('反序列化后的对象: $userMap');

    // 序列化Dart对象为JSON字符串
    String JsonString = jsonEncode(userMap);
    print('序列化后的JSON字符串: $JsonString');

    User user = User.fromJson(userMap);
    print('反序列化后的对象: $user');
  }
}

// class DateTimeConverter implements JsonConverter<DateTime, String> {
//   @override
//   DateTime fromJson(String json) {
//     return DateTime.parse(json);
//   }
//
//   @override
//   String toJson(DateTime object) {
//     return object.toIso8601String();
//   }
// }
